/**
 * @author whailong  [wanghl@ncsi.com.cn]
 * @date 17 Jun 2014 21:06:51
 */
package com.surent.controller.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.app.ActionBar;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;

import com.andreabaccega.widget.FormEditText;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.SaveCallback;
import com.avos.avoscloud.SignUpCallback;
import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.surent.model.dbmodel.House;
import com.surent.model.dbmodel.Image;
import com.surent.model.dbmodel.User;
import com.surent.view.adapter.AdapterSelectedPic;

/**
 * 
 * @author whailong [wanghl@ncsi.com.cn]
 * @date 17 Jun 2014 21:06:51
 */
@ContentView(R.layout.aty_pubhouse)
public class AtyPublishHouse extends SwipeBackActivity {

	@ViewInject(R.id.fet_atyPubHouse_area)
	private FormEditText fetArea;

	@ViewInject(R.id.fet_atyPubHouse_community)
	private FormEditText fetCommunity;

	@ViewInject(R.id.fet_atyPubHouse_describe)
	private FormEditText fetDescribe;

	@ViewInject(R.id.fet_atyPubHouse_houseLayout)
	private FormEditText fetHouseLayout;

	@ViewInject(R.id.fet_atyPubHouse_linkName)
	private FormEditText fetLinkName;

	@ViewInject(R.id.fet_atyPubHouse_linkNum)
	private FormEditText fetLinkNum;

	@ViewInject(R.id.fet_atyPubHouse_price)
	private FormEditText fetPrice;

	@ViewInject(R.id.fet_atyPubHouse_title)
	private FormEditText fetTitle;

	@ViewInject(R.id.rbtnGroup_atyPubHouse_from)
	private RadioGroup rgFrom;

	// @ViewInject(R.id.gv_atyPubHouse_selectedPic)
	// private GridView gvSelectedPic;

	// @ViewInject(R.id.btn_atyPublish_publish)
	// private Button btnPublish;

	private AdapterSelectedPic adapter;
	private ArrayList<String> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);

		list = new ArrayList<String>();
		list.add("");
		adapter = new AdapterSelectedPic(this);
		adapter.addAll(list);
		// gvSelectedPic.setAdapter(adapter);

		// User user = new User();
		// user.setPassword("1234");
		// user.setEmail("65433664@qq.com");
		// user.setUsername("hailong");
		// user.signUpInBackground(new SignUpCallback() {
		//
		// @Override
		// public void done(AVException e) {
		// System.out.println(e);
		// }
		// });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.aty_pubhouse, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == R.id.menu_atyPubHouse_publish) {
			final House house = new House();
			house.setFetchWhenSave(true);
			house.ininData("标题", 12, "一室一厅", 120, 10, 20, "地铁房", "101001", "整租", "精装", "朝南", "压一付一", "园区", "海德公园",
					new AVGeoPoint(20, 20), "小明", "18949932270", "个人", User.getCurrentUser(User.class));
			house.saveInBackground(new SaveCallback() {

				@Override
				public void done(AVException e) {
					System.out.println("上传成功");
					System.out.println(e);
					Image image = new Image();
					try {
						image.initImage(
								AVFile.withAbsoluteLocalPath("keke.jpg", Environment.getExternalStorageDirectory()
										+ File.separator + "1.jpg"), house);
						image.saveInBackground();
					} catch (FileNotFoundException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			});
		}

		if (id == android.R.id.home) {
			this.finish();
		}

		return super.onOptionsItemSelected(item);
	}

	// @OnItemClick(R.id.gv_atyPubHouse_selectedPic)
	// public void picOnClick(AdapterView<?> parent, View view, int position,
	// long id) {
	// if (position == this.list.size() - 1) {
	// }
	// }

}
